<?php
namespace Homework5;

class Maze {
	const TOP 			= 'top';
	const TOPLEFT 		= 'topleft';
	const TOPRIGHT 		= 'topright';
	const BOTTOMLEFT	= 'bottomleft';
	const BOTTOMRIGHT 	= 'bottomright';
	const BOTTOM 		= 'bottom';
	
	private $elements;
	private $radius;
	private $width;
	private $side;
	
	private $positions = array (
		0 => array (
			-1 	=> self::TOPLEFT,
			1 	=> self::TOPRIGHT
		),
		1 => array (
			0 	=> self::BOTTOM,
			-1 	=> self::BOTTOMLEFT,
			1 	=> self::BOTTOMRIGHT
		),
		-1 => array (
			0 	=> self::TOP,
			1 	=> self::TOPRIGHT,
			-1	=> self::TOPLEFT
		)
	);
	
	private $image;
	
	public function __construct() {
		$this->radius = 50;
		$this->width = 2 * $this->radius;
		$this->side = 1.5 * $this->radius;
	}
	
	public function draw($size) {
		$radius = $this->radius;
		$side = $this->side;
		
		$this->image = imagecreatetruecolor(1360,768);

		// Allocate a color for the polygon
		$background = imagecolorallocate($this->image, 255, 255, 255);
		$white = imagecolorallocate($this->image, 255, 255, 255);
		$black = imagecolorallocate($this->image, 0, 0, 0);
		
		imagefilledrectangle($this->image, 0, 0, 1360, 768, $background);

		for ($i = 0; $i < $size; $i++) {
			for ($j = 0; $j < $size; $j++) {
				//$height = $radius * sin(deg2rad(60));
				//if ($j % 2 == 0) {
				$height = $i * 2 * $radius * sin(deg2rad(60));
				//}
				if ($j % 2 != 0) {
					$height = $height + $radius * sin(deg2rad(60));
				}

				imagepolygon($this->image, array (
					0.5 * $radius + $j * $side, $height,
					1.5 * $radius + $j * $side, $height,
					2 * $radius + $j * $side, $radius * sin(deg2rad(60)) + $height,
					1.5 * $radius + $j * $side, 2 * $radius * sin(deg2rad(60)) + $height,
					0.5 * $radius + $j * $side, 2 * $radius * sin(deg2rad(60)) + $height,
					0 + $j * $side, $radius * sin(deg2rad(60)) + $height
					),
					6,
					$black
				);

				$this->elements[] = array($j, $i);
			}
		}
	}
	
	function removeWall($i, $j) {
		if ($i != $j) {
			$a = $this->elements[$i];
			$b = $this->elements[$j];
			$smallest = $a;
			$largest = $b;
			if ($j < $i) {
				$smallest = $b;
				$largest = $a;
			}
			$x = $smallest[0];
			$y = $smallest[1];
			//var_dump($largest);
			//var_dump($smallest);
			//var_dump($this->elements); die();
			list($pos1, $pos2) = array($largest[0]- $smallest[0], $largest[1] - $smallest[1]);
			$position = $this->positions[$pos1][$pos2];
			$this->drawLine($x, $y, $position);
		}

	}
	
	function drawLine($x, $y, $position) {
		$radius = $this->radius;
		$side = $this->side;
		$width = $this->width;
		$white = imagecolorallocate($this->image, 255, 255, 255);
		
		imagesetthickness($this->image, 2);
		
		if ($position == self::TOP) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side + ($width - $side);	
			$y1 = $y2 = $height;
			$x2 = $x1 + $side - ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		} else if ($position == self::TOPLEFT) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side;	
			$y2 = $height;
			$y1 = $height + $radius * sin(deg2rad(60));
			$x2 = $x1 + ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		} else if ($position == self::TOPRIGHT) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side + $side;	
			$y1 = $height;
			$y2 = $height + $radius * sin(deg2rad(60));
			$x2 = $x1 + ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		} else if ($position == self::BOTTOM) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side + ($width - $side);	
			$y1 = $y2 = $height + 2 * $radius * sin(deg2rad(60));
			$x2 = $x1 + $side - ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		} else if ($position == self::BOTTOMLEFT) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side;	
			$y1 = $height + $radius * sin(deg2rad(60));
			$y2 = $height + 2 * $radius * sin(deg2rad(60));
			$x2 = $x1 + ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		} else if ($position == self::BOTTOMRIGHT) {
			$height = $x * 2 * $radius * sin(deg2rad(60));
			if ($y % 2 != 0) {
				$height = $height + $radius * sin(deg2rad(60));
			}
		
			$x1 = $y * $side + $side;	
			$y2 = $height + $radius * sin(deg2rad(60));
			$y1 = $height + 2 * $radius * sin(deg2rad(60));
			$x2 = $x1 + ($width - $side);
			
			imageline($this->image, $x1, $y1, $x2, $y2, $white);
		}
	}
	
	public function getElements() {
		
		return $this->elements;
	}
	
	public function render() {
		// Output the picture to the browser
		header('Content-type: image/png');
		
		imagepng($this->image);
		imagedestroy($this->image);
	}
}